/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controleur;

import com.opensymphony.xwork2.ActionSupport;
import java.util.Date;
import java.util.List;
import java.util.Map;
import modele.NewHibernateUtil;
import modele.Personne;
import modele.Profession;
import org.apache.struts2.interceptor.SessionAware;
import org.hibernate.Session;

/**
 *
 * @author Aimee
 */
public class AjoutProfession extends ActionSupport implements SessionAware {

    public Map<String, Object> session;
    public Session hibernateSession;
    public String nomEntreprise;
    public String poste;
    public String dateDebut;
    public String dateFin;
    public String salaire;
    public String idPersonne;
    public List<Profession> listeProfession;
    
    /**
     * @return the session
     */
    public Map<String, Object> getSession() {
        return session;
    }

    /**
     * @param session the session to set
     */
    public void setSession(Map<String, Object> session) {
        this.session = session;
    }

    /**
     * @return the hibernateSession
     */
    public Session getHibernateSession() {
        return hibernateSession;
    }

    /**
     * @param hibernateSession the hibernateSession to set
     */
    public void setHibernateSession(Session hibernateSession) {
        this.hibernateSession = hibernateSession;
    }
    
    /**
     * @return the nomEntreprise
     */
    public String getNomEntreprise() {
        return nomEntreprise;
    }

    /**
     * @param nomEntreprise the nomEntreprise to set
     */
    public void setNomEntreprise(String nomEntreprise) {
        this.nomEntreprise = nomEntreprise;
    }

    /**
     * @return the poste
     */
    public String getPoste() {
        return poste;
    }

    /**
     * @param poste the poste to set
     */
    public void setPoste(String poste) {
        this.poste = poste;
    }

    /**
     * @return the dateDebut
     */
    public String getDateDebut() {
        return dateDebut;
    }

    /**
     * @param dateDebut the dateDebut to set
     */
    public void setDateDebut(String dateDebut) {
        this.dateDebut = dateDebut;
    }

    /**
     * @return the dateFin
     */
    public String getDateFin() {
        return dateFin;
    }

    /**
     * @param dateFin the dateFin to set
     */
    public void setDateFin(String dateFin) {
        this.dateFin = dateFin;
    }

    /**
     * @return the salaire
     */
    public String getSalaire() {
        return salaire;
    }

    /**
     * @param salaire the salaire to set
     */
    public void setSalaire(String salaire) {
        this.salaire = salaire;
    }
       /**
     * @return the idPersonne
     */
    public String getIdPersonne() {
        return idPersonne;
    }

    /**
     * @param idPersonne the idPersonne to set
     */
    public void setIdPersonne(String idPersonne) {
        this.idPersonne = idPersonne;
    }
    
    public String ajoutProfession() throws Exception{
        
        setHibernateSession(NewHibernateUtil.getSessionFactory().openSession());
        getHibernateSession().beginTransaction();
        Personne pers = (Personne) hibernateSession.createQuery("from Personne where idPersonne =" + getIdPersonne()).uniqueResult();
        Profession profession = new Profession();
        profession.setPersonne(pers);
        profession.setNomEntreprise(nomEntreprise);
        profession.setPoste(poste);
        profession.setSalaire(Double.valueOf(salaire));
        Date anneeDebut = new Date(dateDebut);
        profession.setDateDebut(anneeDebut);
        if(dateFin != null && !(dateFin.trim().equals(""))){
            Date anneeFin = new Date(dateFin);
            profession.setDateFin(anneeFin);
        }
        getHibernateSession().save(profession);
        getHibernateSession().getTransaction().commit();
        listeProfession = hibernateSession.createQuery("from Profession where idPersonne= " + pers.getIdPersonne()).list();
        session.put("listeProfession", listeProfession);
        if(pers.getEstAdmin()){
            return "admin";
        }else{
             return "alumni";
        }
       
    }

    /**
     * @return the listeProfession
     */
    public List<Profession> getListeProfession() {
        return listeProfession;
    }

    /**
     * @param listeProfession the listeProfession to set
     */
    public void setListeProfession(List<Profession> listeProfession) {
        this.listeProfession = listeProfession;
    }

 
}
